<template><div><h2 id="哈希" tabindex="-1"><a class="header-anchor" href="#哈希"><span>哈希</span></a></h2>
<p>Laravel 10 中文文档 /</p>
<h2 id="哈希-1" tabindex="-1"><a class="header-anchor" href="#哈希-1"><span>哈希</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#configuration">配置</a></li>
<li><a href="#basic-usage">基本用法</a>
<ul>
<li><a href="#hashing-passwords">哈希密码</a></li>
<li><a href="#verifying-that-a-password-matches-a-hash">验证密码是否与哈希值相匹配</a></li>
<li><a href="#determining-if-a-password-needs-to-be-rehashed">确定密码是否需要重新哈希</a></li>
</ul>
</li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>Laravel <code v-pre>Hash</code> <a href="https://learnku.com/docs/laravel/10.x/facades" target="_blank" rel="noopener noreferrer">Facad</a> 为存储用户密码提供了安全的 Bcrypt 和 Argon2 哈希。如果您使用的是一个[Laravel 应用程序启动套件](/docs/laravel/10.x/st arter-kits)，那么在默认情况下，Bcrypt 将用于注册和身份验证。</p>
<p>Bcrypt 是哈希密码的绝佳选择，因为它的「加密系数」是可调节的，这意味着随着硬件功率的增加，生成哈希的时间可以增加。当哈希密码时，越慢越好。算法花费的时间越长，恶意用户生成「彩虹表」的时间就越长，该表包含所有可能的字符串哈希值，这些哈希值可能会被用于针对应用程序的暴力攻击中。</p>
<h2 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h2>
<p>你可以在 <code v-pre>config/hashing.php</code> 配置文件中配置默认哈希驱动程序。目前有几个受支持的驱动程序：<a href="https://en.wikipedia.org/wiki/Bcrypt" target="_blank" rel="noopener noreferrer">Bcrypt</a> 和 <a href="https://en.wikipedia.org/wiki/Argon2" target="_blank" rel="noopener noreferrer">Argon2</a>（Argon2i 和 Argon2id 变体）。</p>
<h2 id="基本用法" tabindex="-1"><a class="header-anchor" href="#基本用法"><span>基本用法</span></a></h2>
<h3 id="哈希密码" tabindex="-1"><a class="header-anchor" href="#哈希密码"><span>哈希密码</span></a></h3>
<p>您可以通过在 <code v-pre>Hash</code> Facade 上调用 <code v-pre>make</code> 方法来哈希密码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>RedirectResponse</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Hash</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">PasswordController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 更新用户的密码。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">update</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RedirectResponse</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// 验证新密码的长度...</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">fill</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token class-name static-context">Hash</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token property">newPassword</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/profile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="调整-bcrypt-加密系数" tabindex="-1"><a class="header-anchor" href="#调整-bcrypt-加密系数"><span>调整 Bcrypt 加密系数</span></a></h4>
<p>如果您正在使用 Bcrypt 算法，则 <code v-pre>make</code> 方法允许您使用 <code v-pre>rounds</code> 选项来配置该算法的加密系数。然而，对大多数应用程序来说，默认值就足够了：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$hashed</span> <span class="token operator">=</span> <span class="token class-name static-context">Hash</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'rounds'</span> <span class="token operator">=></span> <span class="token number">12</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="调整-argon2-加密系数" tabindex="-1"><a class="header-anchor" href="#调整-argon2-加密系数"><span>调整 Argon2 加密系数</span></a></h4>
<p>如果您正在使用 Argon2 算法，则 <code v-pre>make</code> 方法允许您使用 <code v-pre>memory</code>，<code v-pre>time</code> 和 <code v-pre>threads</code> 选项来配置该算法的加密系数。然后，对大多数应用程序来说，默认值就足够了：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$hashed</span> <span class="token operator">=</span> <span class="token class-name static-context">Hash</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'memory'</span> <span class="token operator">=></span> <span class="token number">1024</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'time'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'threads'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
有关这些选项的更多信息，请参见 <a href="https://secure.php.net/manual/en/function.password-hash.php" target="_blank" rel="noopener noreferrer">关于 Argon 哈希的官方 PHP 文档</a> 。</p>
</blockquote>
<h3 id="验证密码是否与哈希值相匹配" tabindex="-1"><a class="header-anchor" href="#验证密码是否与哈希值相匹配"><span>验证密码是否与哈希值相匹配</span></a></h3>
<p>由 <code v-pre>Hash</code> Facade 提供的 <code v-pre>check</code> 方法允许您验证给定的明文字符串是否与给定的哈希值一致：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Hash</span><span class="token operator">::</span><span class="token function">check</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'plain-text'</span><span class="token punctuation">,</span> <span class="token variable">$hashedPassword</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// The passwords match...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="确定密码是否需要重新哈希" tabindex="-1"><a class="header-anchor" href="#确定密码是否需要重新哈希"><span>确定密码是否需要重新哈希</span></a></h3>
<p>由 <code v-pre>Hash</code> Facade 提供的 <code v-pre>needsRehash</code> 方法可以为你检查当散列 / 哈希的加密系数改变时，你的密码是否被新的加密系数重新加密过。某些应用程序选择在身份验证过程中执行此检查：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Hash</span><span class="token operator">::</span><span class="token function">needsRehash</span><span class="token punctuation">(</span><span class="token variable">$hashed</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$hashed</span> <span class="token operator">=</span> <span class="token class-name static-context">Hash</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'plain-text'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/hashing/14880" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/ha...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/hashing/14880" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/ha...</a></p>
</blockquote>
</div></template>


